package com.zwh.gittool;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import org.apache.commons.compress.utils.Lists;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.TreeWalk;

import java.io.File;
import java.util.Date;
import java.util.List;

public class JGitUpdate {
    static String path = "D:\\a_work\\mind_cdp";
    static String destPath = "C:\\Users\\郑文豪\\Desktop\\新建文件夹 (8)";
    static String configPath = destPath + "\\config\\config.txt";
    static String startDate = "2024-02-22 00:00:00";
    static String startEnd = "2024-11-01";

    public static void main(String[] args) throws Exception {
        File repoDir = new File(path);

        // 构建仓库
        Repository repository = new FileRepositoryBuilder().findGitDir(repoDir).build();
        Git git = new Git(repository);

        final DateTime since = DateUtil.parseDate(startDate);
        RevFilter between = CommitTimeRevFilter.after(since);
        // 获取日志
        Iterable<RevCommit> logs = git.log().call();
        List<String> paths = Lists.newArrayList();
        List<String> dels = Lists.newArrayList();
        // 遍历提交
        for (RevCommit commit : logs) {

            // 为每个提交创建一个TreeWalk来查看更改的文件
            try (TreeWalk treeWalk = new TreeWalk(repository)) {
                treeWalk.reset();
                treeWalk.setRecursive(true);
                // 将当前commit的树放入树的遍历器中
                treeWalk.addTree(commit.getTree());


                RenameDetector rd = new RenameDetector(repository);
                rd.addAll(DiffEntry.scan(treeWalk));
                //获取到详细记录结果集    在这里就能获取到一个版本号对应的所有提交记录（详情！！！）
                List<DiffEntry> list = rd.compute();
                for (DiffEntry diffEntry : list) {
                    System.out.println(diffEntry.getChangeType()+"---"+diffEntry.getNewPath()+"--------"+diffEntry.getOldPath());
                }
                // 遍历更改的文件
//                while (treeWalk.next()) {
//                String pathString = treeWalk.getPathString() + "===" + s;
////                    if (paths.contains(pathString)) {
////                        continue;
////                    }
////                    File src = new File(path + File.separatorChar + pathString);
////                    if (src.exists()) {
////                        System.out.println("变更:" + pathString);
////                        File dest = new File(destPath + File.separatorChar + src.getName());
////                        FileUtil.copy(src, dest, true);
////                    } else {
////                        System.out.println("删除:" + pathString);
////                        dels.add(pathString);
////                    }
//                paths.add(pathString);
//                }
            }
        }

        FileUtil.writeUtf8Lines(dels, new File(configPath));
        System.out.println(paths);
    }

}